import {timeFormat} from '../../model/utils/common';

export default {
    namespace: 'record',
    state: {
        isEdit: false,
        detail : { },
        list: [],
        carModal : false,
        carList : null,
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        * getCarList({ payload }, { put, call, select }){
            // 查询汽车列表
            const carList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });
            yield put({
                type : 'save',
                payload : {
                    carList
                }
            });
            return carList;
        },
        *init({ payload }, { put, call, select }){
            let detail =  {
                time : timeFormat(Date.now(), 'YYYY-MM-DD')
            };
            const id = payload && payload.id;
            // 获取车辆列表
            const carList = yield put.resolve({
                type: 'getCarList'
            });
            let carNumber = wx.getStorageSync('carNumber');
            if(!carNumber && carList.length){
                carNumber = carList[0].carNumber;
                wx.setStorageSync('carNumber', carNumber);
            }
            // 展示详情
            if(id){
                // 查询汽车列表
                detail = yield put.resolve({
                    type : '$request/post',
                    payload : {
                        url : '/consumption/getMaintenById',
                        data: {id : Number(id)}
                    }
                });
                detail.price = Number(detail.price)/100;
                detail.time = timeFormat(detail.time, 'YYYY-MM-DD');

            }else{
                detail = {
                    ...detail,
                    'carNumber': carNumber,
                    'recordType': 0,
                    'price': '',
                    'desc': '',
                };
            }

            yield put({
                type : 'save',
                payload : {
                    detail,
                    isEdit: !id,
                    location: payload.location
                }
            });
            if(payload.callback){
                payload.callback(detail);
            }
        },

        *submit({ payload }, { put, call, select }){
            const {detail} = yield select(s => s.record);
            const params = {...detail};
            const today = new Date(timeFormat(Date.now(), 'YYYY-MM-DD')).getTime();
            params.price = parseInt(params.price*100);
            params.time = new Date(params.time).getTime() + Date.now() - today;
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/consumption/update',
                    data: params
                }
            });
            if(result){
                yield put.resolve({
                    type : '$msg/showToast',
                    payload: {
                        title: '保存成功'
                    }
                });
                // eslint-disable-next-line no-undef
                const pages = getCurrentPages();
                if(pages.length > 2){
                    yield put.resolve({
                        type : '$router/back',
                    });
                }else{
                    yield put.resolve({
                        type : '$router/redirect',
                        payload: {
                            url : '/pages/recordList/index'
                        }
                    });
                }
            }
        },

        /**
         * 车牌号修改
         * @param {*} param0 
         * @param {*} param1 
         */
        * carNumberChange({ payload }, { put, call, select }){
            const {detail} = yield select(s => s.createMaintain);
            detail.carNumber = payload.carNumber;
            yield put({
                type : 'save',
                payload : {
                    detail,
                    carModal: false
                }
            });
        },


        *delete({ payload }, { put, call, select }){
            const id = payload.id;
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/consumption/delete',
                    data: {id}
                }
            });
            if(result){
                yield put.resolve({
                    type : '$msg/showToast',
                    payload: {
                        title: '删除成功'
                    }
                });
                yield put.resolve({
                    type : '$router/back',
                });
            }
        },
    }
};